/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.filecenter.application.action.auditlog.lisenter;

import cn.hallele.infrastructure.message.io.Message;
import cn.hallele.ms.filecenter.application.action.auditlog.AuditLogApplicationService;
import cn.hallele.ms.infrastructure.auditlog.io.ApiLogIO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Consumer;

/**
 * 日志 消息队列 监听
 *
 * @author anle5
 * @since 2024/9/19 12:31
 */
@Slf4j
@Configuration
public class AuditLogListener {
    @Autowired
    private AuditLogApplicationService auditLogApplicationService;

    /**
     * 消费 auditlog-in-0 binding 队列的消息
     *
     * @return Consumer
     */
    @Bean
    public Consumer<Message<ApiLogIO>> auditlog() {
        return message -> {
            if (message.getMessage() == null) {
                log.error("Receiver a null api log,will ignore it.Message id is : {}, BindingName is : {}",
                        message.getSessionId(), message.getBindingName());
                return;
            }
            auditLogApplicationService.applyApiLog(message.getMessage());
        };
    }
}
